//
//  TestViewController5.m
//  MSDemo
//
//  Created by ms on 2023/12/12.
//

#import "TestViewController5.h"

@interface TestViewController5 ()
<
YYTextViewDelegate
>
ms_keyBoardTool

@end

@implementation TestViewController5

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"YYLabel+YYTextView";
    
    
#pragma mark - YYLabel
    // YYLabel
    YYLabel *label = [[YYLabel alloc] init];
    label.preferredMaxLayoutWidth = self.view.frame.size.width-85;//最大宽度
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:@"测试协议《百度协议》《标签富文本》"];
    // 设置不同字体颜色
    [attriStr yy_setColor:[UIColor blueColor] range:[[attriStr string] rangeOfString:@"《百度协议》"]];
    [attriStr yy_setColor:[UIColor redColor] range:[[attriStr string] rangeOfString:@"《标签富文本》"]];
    
    // 文字点击事件
    // 方式一
    [attriStr yy_setTextHighlightRange:[attriStr.string rangeOfString:@"《百度协议》"]
                             color:[UIColor blueColor]
                   backgroundColor:UIColor.clearColor
                          userInfo:nil
                         tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        NSLog(@"%@", [text.string substringWithRange:range]);
        MSWebViewController *webVC = [[MSWebViewController alloc] init];
        webVC.MSURL = @"https:www.baidu.com";
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC
                                             animated:YES];
    }
                   longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        NSLog(@"%@", [text.string substringWithRange:range]);
    }];
    
    // 方式二
    YYTextHighlight *highlight = [YYTextHighlight new];
    [highlight setColor:[UIColor orangeColor]];
    highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        
        NSString *string = @"<p style=\"text-align: center;\"><b>测试测试</b></p ><p style=\"text-align: center;\"><img src=\"https://img.minweixian.cn/store/ueditor_web_new/1622529221.png\" alt=\"1622529221.png\"></p ><p style=\"text-align: left;\">测试测试的</p >";
        MSWebViewController *webVC = [[MSWebViewController alloc] init];
        webVC.ms_localPath = [string adaptWebViewForHtml];
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC
                                             animated:YES];
    };
    
    [attriStr yy_setTextHighlight:highlight range:[attriStr.string rangeOfString:@"《标签富文本》"]];
    label.attributedText = attriStr;
    
    [self.view addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.navBar.mas_bottom).offset(300);
        make.centerX.equalTo(self.view);
    }];
    
#pragma mark - YYTextView
    // YYTextView
    YYTextView *textView = [[YYTextView alloc] init];
    [textView ms_makeChains:^(MSChainMaker * _Nonnull make) {
        
        make.ms_lineColor(UIColor.redColor)
        .ms_addToSuperView(self.view);
    }];
    textView.delegate = self;
    textView.placeholderText = @"YYTextView";
    textView.font = MS_SYSTEM_FONT_OF_SIZE(15);
    textView.placeholderFont = textView.font;
    textView.textColor = MS_HEX_COLOR(0x333333);
    [self.view addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(label.mas_bottom).offset(200);
        make.left.mas_equalTo(50);
        make.height.mas_equalTo(100);
        make.centerX.equalTo(self.view);
    }];
}


#pragma mark - YYTextViewDelegate
ms_lazyLoadingGetKeyBoardTool
- (BOOL)textViewShouldBeginEditing:(YYTextView *)textView
{
    AUTO_MOVE_TEXTFIELD_UP(textView, self.view, YES)
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
